<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>A reflective class-based system &mdash; The Logtalk Handbook v3.93.0-b01 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=396eccfe" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../_static/documentation_options.js?v=c8100655"></script>
        <script src="../_static/doctools.js?v=9a2dae69"></script>
        <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <!-- begin favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
    <meta name="msapplication-TileColor" content="#355b95" />
    <meta name="theme-color" content="#ffffff" />
    <!-- end favicon -->
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Profiling programs" href="profiling.html" />
    <link rel="prev" title="Dynamic object attributes" href="attributes.html" />
   
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            The Logtalk Handbook
              <img src="../_static/logtalk.gif" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                3.93.0
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
    
              <p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../userman/index.html">User Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../refman/index.html">Reference Manual</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Tutorial</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="lists.html">List predicates</a></li>
<li class="toctree-l2"><a class="reference internal" href="attributes.html">Dynamic object attributes</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">A reflective class-based system</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#defining-the-base-classes">Defining the base classes</a></li>
<li class="toctree-l3"><a class="reference internal" href="#summary">Summary</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="profiling.html">Profiling programs</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devtools/index.html">Developer Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ports/index.html">Ports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributions/index.html">Contributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bibliography.html">Bibliography</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">Index</a></li>
</ul>

    <p class="caption"><span class="caption-text">External Contents</span></p>
    <ul>
    <li class="toctree-l1"><a class="reference internal" href="../../apis/index.html">APIs</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://logtalk.org">Logtalk website</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://github.com/LogtalkDotOrg/logtalk3">GitHub repo</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">The Logtalk Handbook</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="index.html">Tutorial</a></li>
      <li class="breadcrumb-item active">A reflective class-based system</li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/LogtalkDotOrg/logtalk3/blob/master/docs/handbook/sources/tutorial/reflection.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="a-reflective-class-based-system">
<h1>A reflective class-based system<a class="headerlink" href="#a-reflective-class-based-system" title="Link to this heading"></a></h1>
<p>When compiling an object, Logtalk distinguishes prototypes from instances
or classes by examining the object relations. If an object instantiates
and/or specializes another object, then it is compiled as an instance or
class, otherwise it is compiled as a prototype. A consequence of this is
that, in order to work with instance or classes, we always have to
define root objects for the instantiation and specialization hierarchies
(however, we are not restricted to a single hierarchy). The best
solution is often to define a reflective class-based system <a class="reference internal" href="../bibliography.html#maes87" id="id1"><span>[Maes87]</span></a>,
where every class is also an object and, as such, an instance of some class.</p>
<p>In this example, we are going to define the basis for a reflective
class-based system, based on an extension of the ideas presented in
<a class="reference internal" href="../bibliography.html#cointe87" id="id2"><span>[Cointe87]</span></a>. This extension
provides, along with root objects for the instantiation and
specialization hierarchies, explicit support for abstract classes
<a class="reference internal" href="../bibliography.html#moura94" id="id3"><span>[Moura94]</span></a>.</p>
<section id="defining-the-base-classes">
<span id="classes"></span><h2>Defining the base classes<a class="headerlink" href="#defining-the-base-classes" title="Link to this heading"></a></h2>
<p>We will start by defining three classes: <code class="docutils literal notranslate"><span class="pre">object</span></code>, <code class="docutils literal notranslate"><span class="pre">abstract_class</span></code>,
and <code class="docutils literal notranslate"><span class="pre">class</span></code>. The class <code class="docutils literal notranslate"><span class="pre">object</span></code> will contain all predicates common
to all objects. It will be the root of the inheritance graph:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(object,
    <span class="k">instantiates</span>(class)).

    <span class="c">% predicates common to all objects</span>

<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>The class <code class="docutils literal notranslate"><span class="pre">abstract_class</span></code> specializes <code class="docutils literal notranslate"><span class="pre">object</span></code> by adding predicates
common to all classes. It will be the default meta-class for abstract
classes:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(abstract_class,
    <span class="k">instantiates</span>(class),
    <span class="k">specializes</span>(object)).

    <span class="c">% predicates common to all classes</span>

<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>The class <code class="docutils literal notranslate"><span class="pre">class</span></code> specializes <code class="docutils literal notranslate"><span class="pre">abstract_class</span></code> by adding predicates
common to all instantiable classes. It will be the root of the
instantiation graph and the default meta-class for instantiable classes:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(class,
    <span class="k">instantiates</span>(class),
    <span class="k">specializes</span>(abstract_class)).

    <span class="c">% predicates common to all instantiable classes</span>

<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>Note that all three objects are instances of class <code class="docutils literal notranslate"><span class="pre">class</span></code>. The
instantiation and specialization relationships are chosen so that each
object may use the predicates defined in itself and in the other two
objects, with no danger of <a class="reference internal" href="../glossary.html#term-message-lookup"><span class="xref std std-term">message lookup</span></a> endless loops.</p>
</section>
<section id="summary">
<h2>Summary<a class="headerlink" href="#summary" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>An object that does not instantiate or specialize other objects is
always compiled as a prototype.</p></li>
<li><p>An instance must instantiate at least one object (its class).
Similarly, a class must at least specialize or instantiate other
object.</p></li>
<li><p>The distinction between abstract classes and instantiable classes is
an operational one, depending on the class inherited methods. A class
is instantiable if inherits methods for creating instances.
Conversely, a class is abstract if does not inherit any instance
creation method.</p></li>
</ul>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="attributes.html" class="btn btn-neutral float-left" title="Dynamic object attributes" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="profiling.html" class="btn btn-neutral float-right" title="Profiling programs" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1998-2025, Paulo Moura.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>